home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-30 | 8.2 KB | 382 lines | [TEXT/MPS ] |
- /*
- File: OTUdpPitchSample.cp
-
- Contains: UDP pitch sample code
-
- Copyright: © 1993-1997 by Apple Computer, Inc., all rights reserved.
-
- */
-
-
- // OT UDP Pitch Test Program (as an SIOW app)
-
- #include <QuickDraw.h>
- #include <stdio.h>
- #include <StdLib.h>
- #include <TextUtils.h>
- #include <strings.h>
- #include <String.h>
- #include <Events.h>
- #include <Menus.h>
- #include <Devices.h>
- #include <Windows.h>
- #include <OpenTransport.h>
- #include <OpenTptInternet.h>
-
-
- /*******************************************************************************
- ** GLOBAL VARIABLES
- ********************************************************************************/
-
- #define kMaxDataLen 256
-
- InetPort gCatchPort=0;
- InetHost gCatchIpAddr=0;
- InetPort gPitchPort=0;
- InetHost gPitchIpAddr=0;
-
- UInt32 gNoOfTimes=0;
- UInt16 gBindCompleted = 0;
-
- OTEventCode gCode;
- TEndpoint* gCookie;
- OSStatus gErr;
-
- struct InetAddress reqsin, retsin, tmpsin;
- char data[kMaxDataLen];
- char defaultData[] = "Go Cal, beat Stanford !!!";
-
- Boolean gFirstTime = true;
-
- /*******************************************************************************
- ** Function Prototypes
- ********************************************************************************/
-
- void Inits();
- void CleanUp();
- void Idle();
- void DoIt();
- OSStatus DoStaticBind(TEndpoint* ep);
- OSStatus DoSend(TEndpoint* ep);
-
-
- /*******************************************************************************
- ** main function
- ********************************************************************************/
-
- void main(int, char**)
- {
- char userInput[256];
-
- do
- {
- Inits();
- DoIt();
- CleanUp();
-
- fprintf(stderr, "Again (y/n)?\n");
- gets(&userInput[0]);
- } while ( userInput[0] == 'y' || userInput[0] == 'Y' );
-
- fprintf(stderr, "Bye\n");
- exit (0);
- }
-
- /*******************************************************************************
- ** Initialize Quickdraw and ASLM
- ********************************************************************************/
-
- void Inits()
- {
- if ( gFirstTime == true )
- {
- InitGraf(&qd.thePort);
- gFirstTime = false;
- }
-
- if ( InitOpenTransport() != kOTNoError )
- {
- fprintf(stderr, "OTUdpPitch: Could not initialize Open Transport, exiting\n");
- exit(1);
- }
-
- }
-
- /*******************************************************************************
- ** Clean up at the end
- ********************************************************************************/
-
- void CleanUp()
- {
- CloseOpenTransport();
- }
-
- /*******************************************************************************
- ** Idle
- ********************************************************************************/
-
- void Idle()
- {
- SystemTask();
- }
-
- /*******************************************************************************
- ** IsPressed
- ********************************************************************************/
-
- Boolean IsPressed(UInt16 k, KeyMap map)
- {
- UInt8* keyMap = (UInt8*)map;
- return (keyMap[k >> 3] >> (k & 7) & 1);
- }
-
- /*******************************************************************************
- ** CmdKey
- ********************************************************************************/
-
- Boolean UserAbort()
- {
- KeyMap keyMap;
-
- GetKeys(keyMap);
- if ( IsPressed(0x37, keyMap) &&
- (IsPressed(0x2F, keyMap) || IsPressed(0x41, keyMap)) )
- {
- FlushEvents(everyEvent, 0); // needed so that "gets" gets not confused later on
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
- /*******************************************************************************
- ** EventHandler
- ********************************************************************************/
-
- pascal void EventHandler(void*, OTEventCode event, OTResult result, void* cookie)
- {
- if ( event == T_BINDCOMPLETE )
- {
- gBindCompleted = 1;
- }
- else if ( event == T_OPENCOMPLETE )
- {
- gErr = (OSStatus) result;
- gCookie = (TEndpoint*) cookie;
- gCode = event;
- }
- return;
- }
-
- /*******************************************************************************
- ** DoIt
- ********************************************************************************/
-
- void DoIt()
- {
- TEndpoint* ep = NULL;
- TEndpointInfo info;
- OSStatus err=0;
- long myport=0;
- InetHost myaddr=0;
- char mystr[255];
-
- myport = 0;
- fprintf(stderr, "What UDP port should I use to send a data ? (enter UDP port number)\n");
- if (gets(mystr) != 0)
- {
- stringtonum(mystr, &myport);
- gPitchPort =(InetPort) myport;
- }
- myaddr = 0;
- fprintf(stderr, "Where should I send a data ? (enter IP address)\n");
- if (gets(mystr) != 0)
- {
- if (OTInetStringToHost(mystr, &myaddr) == 0)
- {
- gCatchIpAddr = (InetHost) myaddr;
- }
- }
- myport = 0;
- fprintf(stderr, "To which port should I send a data ? (enter port number)\n");
- if (gets(mystr) != 0)
- {
- stringtonum(mystr, &myport);
- gCatchPort = (InetPort) myport;
- }
- fprintf(stderr, "What should I send ? (enter data string)\n");
- if (gets(data) == 0)
- {
- strcpy(data, defaultData);
- fprintf(stderr, "send default data: <%s>\n", data);
- }
- myport = 0;
- fprintf(stderr, "How many times should I send this data ?\n");
- if (gets(mystr) != 0)
- {
- stringtonum(mystr, &myport);
- gNoOfTimes = (UInt16)myport;
- }
-
- OTInetHostToString(gCatchIpAddr, mystr);
- fprintf(stderr, "The program will send <%d> packets to <%s:%d> on port <%d>\n", gNoOfTimes, mystr, gCatchPort, gPitchPort);
- OTInitInetAddress(&reqsin, gPitchPort, (InetHost) 0);
-
- do
- {
- //
- // Now create a UDP
- //
- #if 0
- gCode = 0;
- gCookie = NULL;
- gErr = 0;
- err = OTAsyncOpenEndpoint(OTCreateConfiguration(kUDPName), 0,
- &info, EventHandler, 0);
- if ( err == 0 )
- {
- while ( gCode == 0 )
- OTIdle();
- err = gErr;
- }
- if ( err != 0 )
- {
- ep = NULL;
- fprintf(stderr,"ERROR: OpenEndpoint(\"UDP\") failed with %d\n", err);
- break;
- }
- else
- {
- ep = gCookie;
- }
- #else
- ep = OTOpenEndpoint(OTCreateConfiguration(kUDPName), 0, &info, &err);
-
- if ( ep == NULL || err != kOTNoError )
- {
- ep = NULL;
- fprintf(stderr,"ERROR: OpenEndpoint(\"UDP\") failed with %d\n", err);
- break;
- }
- //
- // Install notifier we're going to use for testing
- //
- err = ep->InstallNotifier(EventHandler, 0);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
- break;
- }
- #endif
- //
- // Try to bind
- //
- ep->SetAsynchronous();
- err = DoStaticBind(ep);
- if ( err != kOTNoError )
- break;
-
- ep->SetSynchronous();
-
- while ( gNoOfTimes-- && !UserAbort() )
- {
- err = DoSend(ep);
- if ( err != kOTNoError )
- break;
- Idle();
- }
- //
- // Try to Unbind
- //
- err = ep->Unbind();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
- break;
- }
- } while (false);
-
- if (ep)
- {
- //
- // Remove notifier
- //
- ep->RemoveNotifier();
- //
- // Get rid of endpoint.
- //
- err = OTCloseProvider(ep);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
- }
- }
- }
-
- /*******************************************************************************
- ** DoStaticBind
- ********************************************************************************/
-
- OSStatus DoStaticBind(TEndpoint* ep)
- {
- TBind req, ret;
- OSStatus err;
-
- // bind udp to current address and port
- req.addr.len = sizeof(struct InetAddress);
- req.addr.buf = (UInt8*) &reqsin;
- req.qlen = 1; // don't care for udp
- ret.addr.maxlen = sizeof(struct InetAddress);
- ret.addr.buf = (UInt8*) &retsin;
-
- err = ep->Bind(&req, &ret);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "Static Bind returns %d\n", err);
- return err;
- }
- while ( gBindCompleted == 0 )
- {
- }
- gBindCompleted = 0;
- // gPitchIpAddr = reqsin.fPort;
- return err;
- }
-
- /*******************************************************************************
- ** DoSend
- ********************************************************************************/
-
- OSStatus DoSend(TEndpoint* ep)
- {
- OSStatus err = kOTNoError;
- TUnitData unitdata;
- char mystr[255];
-
- // Send a UDP datagram
-
- OTInitInetAddress(&tmpsin, gCatchPort, gCatchIpAddr);
-
- unitdata.addr.len = sizeof(struct InetAddress);
- unitdata.addr.buf = (UInt8*) &tmpsin;
- unitdata.opt.len = 0;
- unitdata.opt.buf = 0;
- unitdata.udata.len = strlen(data);
- unitdata.udata.buf = (UInt8*) data;
-
- err = ep->SndUData( &unitdata);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "SndUData() returns %d\n", err);
- }
- else
- {
- OTInetHostToString(gCatchIpAddr, mystr);
- fprintf(stderr, "Sent <%s> to <%s:%d>\n", data, mystr, gCatchPort);
- }
- return err;
- }
-